//
//  DHLeftMenuView.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/1/10.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHLeftMenuView.h"
#import "DHLeftControllerTableViewCell.h"
#import "DHFashionWearViewController.h"
#import "DHSettingViewController.h"
#import "DHUserManagerViewController.h"
#import "DHS2_OrderManagerController.h"
#import "DHCollectionViewController.h"
#import "DHNoticeViewController.h"
#import "DHCustomerViewController.h"
#import "JPUSHService.h"
#import "DHQuestionViewController.h"

#import <ShareSDKExtension/ShareSDK+Extension.h>

#import "DHUser_HomePageController.h"
#import "DHSubscribeManagerController.h"
#import "DHWashingController.h"
#import "DHMyCouponsController.h"

@interface DHLeftMenuView()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIButton *vipButton;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSArray *dataArr;
@property (strong, nonatomic) NSArray *iconImgsArr;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topStackView_topLayout;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *settingButton_topLayout;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *logoutButton_bottomLayout;
@property (weak, nonatomic) IBOutlet UIButton *manButton;
@property (weak, nonatomic) IBOutlet UIButton *womanButton;

@end

@implementation DHLeftMenuView

+ (DHLeftMenuView *)shareDHLeftMenuView{
    DHLeftMenuView *view = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([DHLeftMenuView class]) owner:self options:nil] objectAtIndex:0];
    view.frame = CGRectMake(-kScreenWidth/3 * 2, 0, kScreenWidth/3 * 2, kScreenHeight);
    return view;
}

- (NSArray *)dataArr{
    if (!_dataArr) {
        _dataArr = @[@[@"社交主页",@"订单管理",@"订阅管理",@"衣服清洗",@"资产优惠"],@[@"客服",@"收藏",@"常见问题"]];
    }
    return _dataArr;
}

- (NSArray *)iconImgsArr{
    if (!_iconImgsArr) {
        _iconImgsArr = @[@[@"menu_homepage",@"menu_cart",@"menu_3",@"menu_4",@"menu_5"],@[@"menu_6",@"menu_7",@"menu_8"]];
    }
    return _iconImgsArr;
}

- (void)awakeFromNib{
    [super awakeFromNib];
    
    if (iPhoneX) {
        self.topStackView_topLayout.constant = 45;
        self.settingButton_topLayout.constant = 33;
        self.logoutButton_bottomLayout.constant = 35;
    }
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.tableView registerNib:[UINib nibWithNibName:@"DHLeftControllerTableViewCell" bundle:nil] forCellReuseIdentifier:kDHLeftControllerTableViewCell];
    
    WeakSelf(weakSelf)
    [self.manButton bk_addEventHandler:^(id sender) {
        [[DHNetAPIClient shareDHNetAPIClient] request_user_sexWithParams:@{@"sex":@"男"} andBlock:^(id data, NSError *error) {
            [weakSelf.manButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [weakSelf.womanButton setTitleColor:[UIColor colorWithHexString:@"#b1b1b1"] forState:UIControlStateNormal];
            [[GlobalManager shareGlobalManager] refreshBasicInfoAndBlock:^(id data, NSError *error) {
                postN(@"left_change_sex");
            }];
        }];
    } forControlEvents:UIControlEventTouchUpInside];
    
    [self.womanButton bk_addEventHandler:^(id sender) {
        [[DHNetAPIClient shareDHNetAPIClient] request_user_sexWithParams:@{@"sex":@"女"} andBlock:^(id data, NSError *error) {
            [weakSelf.womanButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [weakSelf.manButton setTitleColor:[UIColor colorWithHexString:@"#b1b1b1"] forState:UIControlStateNormal];
            [[GlobalManager shareGlobalManager] refreshBasicInfoAndBlock:^(id data, NSError *error) {
                postN(@"left_change_sex");
            }];
        }];
    } forControlEvents:UIControlEventTouchUpInside];
}

- (void)updateNotificationsUnreadCount{
    [self.tableView reloadData];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    [self.tableView reloadData];
    
    [self.headImageView dh_setImageURLString:[GlobalManager shareGlobalManager].user.photo];
    if ([GlobalManager shareGlobalManager].user.nickname.length > 0) {
        self.nameLabel.text = [GlobalManager shareGlobalManager].user.nickname;
    }else{
        self.nameLabel.text = [NSString hindPhoneString4Char:[GlobalManager shareGlobalManager].user.phone] ;
    }
    if ([GlobalManager shareGlobalManager].vip.vip) {
        NSLog(@"是vip");
        self.vipButton.hidden = NO;
        [self.vipButton setTitle:[NSString stringWithFormat:@" %@",[[GlobalManager shareGlobalManager].vip.info objectForKey:@"name"]] forState:UIControlStateNormal];
    }else{
        NSLog(@"不是vip");
        self.vipButton.hidden = YES;
    }
    
    if ([[GlobalManager shareGlobalManager].user.sex isEqualToString:@"男"]) {
        [self.manButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.womanButton setTitleColor:[UIColor colorWithHexString:@"#b1b1b1"] forState:UIControlStateNormal];
    }else{
        [self.manButton setTitleColor:[UIColor colorWithHexString:@"#b1b1b1"] forState:UIControlStateNormal];
        [self.womanButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
}

- (IBAction)settingAction:(id)sender {
    DHSettingViewController *settingController = [[DHSettingViewController alloc] init];
    settingController.hidesBottomBarWhenPushed = YES;
    [self.viewController.navigationController pushViewController:settingController animated:YES];
}

- (IBAction)logoutAction:(id)sender {
    [UIAlertView bk_showAlertViewWithTitle:@"登出账户" message:@"您确定要登出账户？Dresshere会不定期提供特惠惊喜哦！" cancelButtonTitle:@"取消" otherButtonTitles:@[@"登出"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
        if (buttonIndex == 0) {
            [alertView setHidden:YES];
        }else{
            [Login doLogout];
            //                [[QYSDK sharedSDK] logout:^(){}];
            [JPUSHService setTags:[NSSet setWithObject:@"IOS"] aliasInbackground:nil];
            [UserSelectedManager shareUserSelectedManager].category = nil;
            [UserSelectedManager shareUserSelectedManager].scenario = nil;
            [kAppDelegate configLoginController];
            [self deleteCookie];
        }
    }];
}

#pragma mark 删除cookie
- (void)deleteCookie
{
    NSLog(@"============删除cookie===============");
    NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
    
    //删除cookie
    for (NSHTTPCookie *tempCookie in cookies) {
        [cookieStorage deleteCookie:tempCookie];
    }
    
    //把cookie打印出来，检测是否已经删除
    NSArray *cookiesAfterDelete = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
    for (NSHTTPCookie *tempCookie in cookiesAfterDelete) {
        NSLog(@"cookieAfterDelete: %@", tempCookie);
    }
    NSLog(@"\n");
}

- (IBAction)userManagerAction:(id)sender {
    DHUserManagerViewController *umController = [[DHUserManagerViewController alloc] init];
    [self.viewController.navigationController pushViewController:umController animated:YES];
}

#pragma mark - UITableViewDataSource and UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataArr[section] count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [self.dataArr count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DHLeftControllerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHLeftControllerTableViewCell forIndexPath:indexPath];
    cell.numberLabel.hidden = YES;
    cell.titleLabel.text = self.dataArr[indexPath.section][indexPath.row];
    [cell.iconImageView setImage:[UIImage imageNamed:self.iconImgsArr[indexPath.section][indexPath.row]]];
    if (indexPath.section == 1 && indexPath.row == 1) {
        if ([GlobalManager shareGlobalManager].notificationsUnreadCount > 0) {
            cell.numberLabel.hidden = NO;
            cell.numberLabel.text = [NSString stringWithFormat:@"%ld",(long)[GlobalManager shareGlobalManager].notificationsUnreadCount];
        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0 && indexPath.row == 0) {
        DHUser_HomePageController *vc = [[DHUser_HomePageController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.viewController.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.section == 0 && indexPath.row == 1){
        DHS2_OrderManagerController *omController = [[DHS2_OrderManagerController alloc] init];
        omController.hidesBottomBarWhenPushed = YES;
        [self.viewController.navigationController pushViewController:omController animated:YES];
    }else if (indexPath.section == 0 && indexPath.row == 2){
        DHSubscribeManagerController *omController = [[DHSubscribeManagerController alloc] init];
        omController.hidesBottomBarWhenPushed = YES;
        [self.viewController.navigationController pushViewController:omController animated:YES];
    }else if (indexPath.section == 0 && indexPath.row == 3){
        DHWashingController *omController = [[DHWashingController alloc] init];
        omController.hidesBottomBarWhenPushed = YES;
        [self.viewController.navigationController pushViewController:omController animated:YES];
    }else if (indexPath.section == 0 && indexPath.row == 4){
        DHMyCouponsController *vc = [[DHMyCouponsController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.viewController.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.section == 1 && indexPath.row == 1){
        DHCollectionViewController *collectionController = [[DHCollectionViewController alloc] init];
        collectionController.hidesBottomBarWhenPushed = YES;
        [self.viewController.navigationController pushViewController:collectionController animated:YES];
    }else if (indexPath.section == 1 && indexPath.row == 0){
        DHCustomerViewController *vc = [[DHCustomerViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.viewController.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.section == 1 && indexPath.row == 2){
        DHQuestionViewController *questionController = [[DHQuestionViewController alloc] init];
        questionController.hidesBottomBarWhenPushed = YES;
        [self.viewController.navigationController pushViewController:questionController animated:YES];
    }
}

//section头部间距
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 1;//section头部高度
}
//section头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
    view.backgroundColor = [UIColor clearColor];
    return view ;
}
//section底部间距
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 40;
}
//section底部视图
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
    view.backgroundColor = [UIColor clearColor];
    if (section == 0) {
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 19.5, self.width, 1)];
        lineView.backgroundColor = RGBAlpha(0, 0, 0, 0.1);
        [view addSubview:lineView];
    }
    return view;
}


@end
